package top.codecrab.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import top.codecrab.springcloud.entity.Payment;
import top.codecrab.springcloud.entity.response.Result;
import top.codecrab.springcloud.service.PaymentService;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author codecrab
 * @since 2021年05月14日 10:40
 */
@Slf4j
@RestController
@RequestMapping("/payment")
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/save")
    public Result save(@RequestBody Payment payment) {
        int result = paymentService.save(payment);

        return result > 0 ?
                Result.ok("保存订单成功，端口号：" + serverPort, result) :
                Result.fail("保存订单失败，端口号：" + serverPort);
    }

    @GetMapping("/get/{id}")
    public Result getById(@PathVariable("id") Long id) {
        Payment payment = paymentService.getById(id);

        return payment != null ?
                Result.ok("查询到订单，端口号：" + serverPort, payment) :
                Result.fail("未找到订单，端口号：" + serverPort);
    }

    @GetMapping(value = "/lb")
    public String getPaymentLB() {
        return serverPort;
    }

    @GetMapping("/feign/timeout")
    public String paymentFeignTimeOut() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }

}
